﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrmAuto;
using System.Web.Security;

public partial class Controls_PassRecoveryControl : System.Web.UI.UserControl
{
		QueryUtenti mQueryUt = null;
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void SubmitButton_Click(object sender, EventArgs e)
		{
		 if( string.IsNullOrEmpty( myTxtEmail.Text + myTxtUserName.Text))return ;
		 
		 mQueryUt =new QueryUtenti();
		 Security sec= new Security();		  
		 Users utente =null;
		   						  
				try 
				{
				  string error=string.Empty ;
				  
						if(! string.IsNullOrEmpty(myTxtUserName.Text))
						{
								if(sec.CiSonoCarNonAmmessi(myTxtUserName.Text,out error ))
								{ FailureText.Text = "Carattere non ammesso : "+ error ;return ;  }
								utente = mQueryUt.getUtenteByUserName(myTxtUserName.Text);
						}		
						 
						if(!string.IsNullOrEmpty( myTxtEmail.Text))
						{
							if(! sec.EmailIsCorrect(myTxtEmail.Text,out error ))
								{ FailureText.Text = error ;return ;  }
								utente = mQueryUt.getUtenteByEmail(myTxtEmail.Text);
						}	
								
						if(utente == null) {this.FailureText.Text ="Utente non trovato con i dati immessi..."; return ;}
										 
										string email = utente.Email ;
										string userName = utente.UserName ; 
										string newPass = mQueryUt.getRandomPassword(6,1);
										mQueryUt.setPasswordUtente(userName,newPass);   
										  
										Mailer mailer = new Mailer();
										 
										mailer.sendLinkRecuperoPassWord(email,userName,newPass); 
										
										this.FailureText.Text = "Password inviata.Controlla la tua email.";    
						
				}
				catch (Exception ex)
				{ this.FailureText .Text = "Errore : "+ex.Message ; }

		}//fM

}//fC
